﻿using System;
using System.Collections.Generic;
using System.Linq;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Services;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Shared.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class UserNotificationsViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        private ILoanRepository _LoanRepository;
        /// <summary>
        /// 
        /// </summary>
        private IMembershipRepository _MembershipRepository;
        /// <summary>
        /// 
        /// </summary>
        private IAdministrationRepository _AdministrationRepository;
        /// <summary>
        /// 
        /// </summary>
        IWorkflowService _WorkflowService;
        /// <summary>
        /// Gets the max waiating months.
        /// </summary>
        /// <value>
        /// The max waiating months.
        /// </value>
        private int MaxWaitingMonths
        {
            get
            {
                string maxPostponeTime = _AdministrationRepository.GetSettingValue(RegistryInfo.CommitteeID, SettingKeys.MaxAllowedPostponeMonths);
                return int.Parse(maxPostponeTime);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UserNotificationsViewModel"/> class.
        /// </summary>
        /// <param name="LoanRepository">The loan repository.</param>
        /// <param name="MembershipRepository">The membership repository.</param>
        /// <param name="AdministrationRepository">The administration repository.</param>
        /// <param name="workflowService">The workflow service.</param>
        public UserNotificationsViewModel(ILoanRepository LoanRepository, IMembershipRepository MembershipRepository,
            IAdministrationRepository AdministrationRepository, IWorkflowService workflowService)
        {
            _LoanRepository = LoanRepository;
            _MembershipRepository = MembershipRepository;
            _AdministrationRepository = AdministrationRepository;
            _WorkflowService = workflowService;

        }

        /// <summary>
        /// Gets the loans without research exceeded delay limit for user.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns></returns>
        public int GetLoansWithoutResearchExceededDelayLimitForUser(Guid userID)
        {
            IEnumerable<IwfStepInstance> allPostponedLoans = _WorkflowService.GetInstancesInStep(Services.WorkflowEngine.Steps.PostponeListWithoutResearchandStudy);
            return allPostponedLoans.Where(sInstance => sInstance.CreationDate.AddMonths(MaxWaitingMonths) > DateTime.Now).Count();
        }

        /// <summary>
        /// Gets the loans with research exceeded delay limit for user.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns></returns>
        public int GetLoansWithResearchExceededDelayLimitForUser(Guid userID)
        {
            IEnumerable<IwfStepInstance> allPostponedLoans = _WorkflowService.GetInstancesInStep(Services.WorkflowEngine.Steps.PostponeListWithResearchandStudy);
            return allPostponedLoans.Where(sInstance => sInstance.CreationDate.AddMonths(MaxWaitingMonths) > DateTime.Now).Count();
        }

        /// <summary>
        /// Gets the loans without research exceeded reminder date for user.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns></returns>
        public int GetLoansWithoutResearchExceededReminderDateForUser(Guid userID)
        {
            IEnumerable<IwfStepInstance> allPostponedLoans = _WorkflowService.GetInstancesInStep(Services.WorkflowEngine.Steps.PostponeListWithoutResearchandStudy);
            return allPostponedLoans.Where(sInstance => sInstance.CompletionDate >= DateTime.Now).Count();
        }

        /// <summary>
        /// Gets the loans with research exceeded reminder date for user.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns></returns>
        public int GetLoansWithResearchExceededReminderDateForUser(Guid userID)
        {
            IEnumerable<IwfStepInstance> allPostponedLoans = _WorkflowService.GetInstancesInStep(Services.WorkflowEngine.Steps.PostponeListWithResearchandStudy);
            return allPostponedLoans.Where(sInstance => sInstance.CompletionDate >= DateTime.Now).Count();
        }


    }
}
